<?php
class ImageCategory extends AppModel{
	public $name='ImageCategory';	
	/*
	public $validate = array(
		'title' => array(array('rule'=>'notEmpty','message'=>'Can not be empty!'),
						array('rule'=>array('unique','title'),'message'=>'data is already existed'))		
	);*/
	public $actsAs = array('Translate'=>array('title'));

	//public $translateTable  = 'news_i18n';
	public $hasMany = array(
        'Image' => array(
            'className'     => 'Image', 
            'dependent'=> true
        )
    );  
    
	// using app/Model/ProductCategory.php
	// In the following example, do not let a product category be deleted if it still contains products.
	// A call of $this->Product->delete($id) from ProductsController.php has set $this->id .
	// Assuming 'ProductCategory hasMany Product', we can access $this->Product in the model.
	public function beforeDelete($cascade = true) {
		//unbound
		App::uses('FileManager', 'Model');
		$FileManager= new FileManager();
		$data=$this->findById($this->id);				
		$FileManager->updateAll(array('mount'=>'mount - 1'),array('name'=>$data[$this->name]['image']));
		
		
	   return true;
	}
    
    function _findTranslated($state, $query, $results = array()) { 
        if ($state == 'before') { 
            return array_merge($query, array( 
                'fields' => array('id', 'name'), 
                'recursive' => -1 
            )); 
        } elseif ($state == 'after') { 
            if (empty($results)) { 
                return $results; 
            } 
            $ids = Set::extract($results, '{n}.Category.id'); 
            $data = $this->Subcategory->find('all', array( 
                'conditions' => array( 
                    'Subcategory.category_id' => $ids 
                ), 
                'fields' => array('id', 'category_id', 'name'), 
                'recursive' => -1 
            )); 
            if (empty($data)) { 
                return $results; 
            } 
            $data = Set::combine( 
                $data, 
                '{n}.Subcategory.id', 
                '{n}.Subcategory', 
                '{n}.Subcategory.category_id' 
            ); 
            $insert = array(); 
            foreach ($data as $category_id => $subcategories) { 
                $insert[$category_id] = array('Subcategory' => 
$subcategories); 
            } 
            $results = Set::combine($results, '{n}.Category.id', 
'{n}'); 
            $results = Set::pushDiff($results, $insert); 
            return $results; 
        } 
    }
}
?>
